package com.rongshu.view.login.activity;

import android.content.Intent;
import android.text.Editable;
import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.rongshu.R;
import com.rongshu.utils.Common;
import com.rongshu.utils.Constant;
import com.rongshu.utils.UserInfoUtils;
import com.rongshu.utils.VerificationUtils;
import com.rongshu.view.base.BaseActivity;
import com.rongshu.view.login.contract.ResetPasswordView;
import com.rongshu.view.login.entity.PasswordLogin;
import com.rongshu.view.login.entity.ResetPassword;
import com.rongshu.view.login.entity.UpdateUserClass;
import com.rongshu.view.login.presenter.ResetPasswordPresenter;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import butterknife.OnTextChanged;

/**
 * 找回密码
 * 接受传参：
 * Constant.INTENT_MESSAGE string 手机号
 * Constant.INTENT_DATA string 短信验证码
 */
public class ResetPasswordActivity extends BaseActivity<ResetPasswordPresenter> implements ResetPasswordView {
    @BindView(R.id.txt_top_title)
    TextView txt_top_title;//标题
    @BindView(R.id.edtNewsPassword)
    EditText edtNewsPassword;
    @BindView(R.id.txtFinishLogin)
    TextView txtFinishLogin;
    @BindView(R.id.show_pwd_switch)
    ImageView showPwdSwitch;

    //intent
    private String phone = null;
    private String code = null;
    //获取验证码type
    private int questAuthCodeType;
    //new_password
    private String new_password = null;
    //intent
    private Intent intent = null;
    //是否显示密码
    private boolean isShowPwd = false;

    @Override
    protected int initLayout() {
        return R.layout.activity_reset_password_view;
    }

    @Override
    protected void initView() {
        //获取意图数据
        phone = getIntent().getStringExtra(Constant.INTENT_MESSAGE);
        code = getIntent().getStringExtra(Constant.INTENT_DATA);
        questAuthCodeType = getIntent().getIntExtra(Constant.INTENT_TYPE, Constant.FIND_PWD);
        //设置title
        txt_top_title.setText((questAuthCodeType == Constant.FIND_PWD) ?
                getString(R.string.reset_password) : getString(R.string.modify_password));
        //presenter
        setPresenter(new ResetPasswordPresenter(this), this);
        txtFinishLogin.setClickable(false);
        txtFinishLogin.setText(questAuthCodeType == Constant.FIND_PWD ? "完成并登录" : "完成");
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void updateViews(boolean isRefresh) {

    }

    @OnTextChanged(value = R.id.edtNewsPassword, callback = OnTextChanged.Callback.AFTER_TEXT_CHANGED)
    void pwdTextChanged(Editable editable) {
        txtFinishLogin.setBackgroundResource(TextUtils.isEmpty(editable.toString()) ?
                R.drawable.shape_btn_8fe49d : R.drawable.shape_btn_3cc051);
        txtFinishLogin.setClickable(!TextUtils.isEmpty(editable.toString()));
    }

    @OnClick({R.id.layout_back, R.id.txtFinishLogin, R.id.show_pwd_switch})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.layout_back:
                showKeyboard(false);
                finish();
                break;
            case R.id.txtFinishLogin:
                new_password = edtNewsPassword.getText().toString().trim();
                if (TextUtils.isEmpty(new_password)) {
                    toast(getString(R.string.news_password_null_error));
                    return;
                } else {
                    if (new_password.length() < 8 || new_password.length() > 20 || !VerificationUtils.isContainAll(new_password)) {
                        toast(getString(R.string.news_password_input_hint));
                        return;
                    }
                }
                //忘记密码，设置密码
                Map<String, Object> map = new HashMap<>();
                map.put("mobile", phone);
                map.put("sms_code", code);
                //body
                UpdateUserClass updateUserClass = new UpdateUserClass();
                updateUserClass.setBi_name("");
                updateUserClass.setPwd(new_password);
                presenter.postResetPassword(
                        (String) map.get("mobile"),
                        (String) map.get("sms_code"),
                        Common.encryptionSign(map),
                        updateUserClass);
                break;
            case R.id.show_pwd_switch:
                if (isShowPwd) {
                    //默认状态显示密码--设置文本 要一起写才能起作用 InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD
                    //mEtPassword.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                    isShowPwd = false;
                    showPwdSwitch.setImageResource(R.drawable.eye_close_icon);
                    edtNewsPassword.setTransformationMethod(PasswordTransformationMethod.getInstance());
                } else {
                    //选择状态 显示明文--设置为可见的密码
                    //mEtPassword.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                    isShowPwd = true;
                    showPwdSwitch.setImageResource(R.drawable.eye_open_icon);
                    //此时隐藏，进行显示
                    edtNewsPassword.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                }
                edtNewsPassword.setSelection(edtNewsPassword.getText().toString().length());
                break;
        }
    }

    /**
     * 重置密码返回结果
     */
    @Override
    public void onResetPassword(ResetPassword resetPassword) {
        if (resetPassword.getErrCode() == 0) {
            if (questAuthCodeType == Constant.CHANGE_PWD) {
                showKeyboard(false);
                setResult(RESULT_OK);
                finish();
                return;
            }
            presenter.getUserRegisterforMobilePwd(phone, new_password);
        } else {
            toast(resetPassword.getErrMsg());
        }
    }

    @Override
    public void getUserRegisterforMobilePwd(PasswordLogin passwordLogin) {
        if (passwordLogin.getErrCode() == 0) {
            showKeyboard(false);
            UserInfoUtils.saveUserInfo(passwordLogin);
            setResult(RESULT_OK);
            finish();
        } else {
            toast(passwordLogin.getErrMsg());
        }
    }
}
